package com.hoopchina.util.download;

import java.util.LinkedList;
import java.util.Queue;

import com.hoopchina.util.LogUtils;
import com.hoopchina.util.download.DownloadMgr.Status;

public class DownloadQueue {

    private Queue<DownloadTask> mQueue = null;
    private Status              mStatus = Status.PENDING;
    private String              mType ="";
    private String              mDownloadUrl = "";
    private String              mUrl = "";
    private int                 mQueueSize = 0; //保存不包括图片任务在内的所有任务总数
    private int                 mFinishedSize = 0;

    public DownloadQueue(String aUrl,String aDownloadUrl, String aType) {
    	mUrl = aUrl;
    	mDownloadUrl = aDownloadUrl;
        mType = aType;
        mQueue = new LinkedList<DownloadTask>();
    }
    
    public boolean offer(DownloadTask taskObject){
        return this.mQueue.offer( taskObject );
    }
    
    public boolean add(DownloadTask taskObject){
        return this.mQueue.add( taskObject );
    }
    
    public void clear(){
        this.mQueue.clear();
    }
    
    public DownloadTask poll(){
        return this.mQueue.poll();
    }
    
    public DownloadTask peek(){
        return this.mQueue.peek();
    }
    
    public int size(){
        return this.mQueue.size();
    }
    
    public boolean contains(DownloadTask taskObject){
        return this.mQueue.contains( taskObject );
    }
    
    public boolean isEmpty(){
        return this.mQueue.isEmpty();
    }
    
    public DownloadTask getFisrtPending() {
        LogUtils.d( "in" );
        DownloadTask pausedTask = null;
        for ( int i = 0; i < mQueue.size(); i++ )
            while ( mQueue.iterator().hasNext() ) {
                DownloadTask tmpTask = ( DownloadTask ) mQueue.iterator().next();
                if ( !tmpTask.getStatus() ) {
                    pausedTask = tmpTask;
                    break;
                }
            }
        return pausedTask;
    }

    public void setType( String aType ) {
        mType = aType;
    }

    public String getType() {
        return mType;
    }

    public void setStatus( Status aStatus ) {
        this.mStatus = aStatus;
    }

    public Status getStatus() {
        return mStatus;
    }

    public void setDownloadUrl( String aUrl ) {
        this.mDownloadUrl = aUrl;
    }

    public String getDownloadUrl() {
        return mDownloadUrl;
    }
    
    public void setUrl( String aUrl ) {
        this.mUrl = aUrl;
    }

    public String getUrl() {
        return mUrl;
    }

    public void setQueueSize( int aQueueSize ) {
        LogUtils.d( "queue size change to: "+aQueueSize );
        this.mQueueSize = aQueueSize;
    }

    
    public int getQueueSize() {
        LogUtils.d( "queue size: "+mQueueSize );
        return this.mQueueSize;
    }
    
    /**
     * 判断aTask是否为队列中的最后一个
     * @param aTask
     * @return
     */
    public boolean isLastTask( ) {
        
        return this.mQueue.peek() == null ? true : false;
    }

    public void setFinishedSize( int mFinishedSize ) {
        this.mFinishedSize = mFinishedSize;
    }

    public int getFinishedSize() {
        return mFinishedSize;
    }

}